/**
 * 询问框
 * @param text
 * @param href 链接地址或函数
 * @param title
 * @param okBtnText 确认按钮文本，选填
 * @param noBtnText 取消按钮文本，选填
 * @returns {Boolean}
 */
function dialogConfirm(text, href, title, okBtnText, noBtnText){
	title = CommUtil.setDefValue(title, "提示");
	SwwetAlert.alertConfirm(title, text, href);
	//LayAlert.alertConfirm(text, href, title, okBtnText, noBtnText);
}


/**
 * @api http://mishengqiang.com/sweetalert/
 * @api http://mishengqiang.com/sweetalert2/
 * @type {{alert: Function, alertText: Function, alertSuccess: Function, alertHtml: Function, alertConfirm: Function, alertAutoClose: Function, alertPrompt: Function}}
 */
var SwwetAlert = {

	// type: warning，error，success，info和question
	alert: function(title) {
		swal({
			title: title,
			buttonsStyling: false,
			confirmButtonClass: "btn btn-success"
		});
	},
	alertText: function(title, text) {
		swal({
			title: title,
			text: text,
			buttonsStyling: false,
			confirmButtonClass: "btn btn-info"
		});
	},
	alertSuccess: function(title, text) {
		swal({
			title: title,
			text: text,
			buttonsStyling: false,
			confirmButtonClass: "btn btn-success",
			type: "success"
		});
	},
	alertHtml: function(title, html) {
		swal({
			title: title,
			buttonsStyling: false,
			confirmButtonClass: "btn btn-success",
			html: html
		});
	},
	alertConfirm: function(title, text, callback) {
		swal({
			title: title,
			text: text,
			type: 'warning',
			showCancelButton: true,
			confirmButtonClass: 'btn btn-success',
			cancelButtonClass: 'btn btn-danger',
			confirmButtonText: '是的，我确定!',
			buttonsStyling: false
		}).then(function() {
			if(typeof callback == 'function'){
				return callback();
			}else{
				window.location.href = callback;
			}
		});
	},
	alertAutoClose: function(title, type, text, closeTime, align) {
		//success/error/warning/md5/bad/nice
		closeTime = CommUtil.setDefValue(closeTime, 2500);
		align = CommUtil.setDefValue(align, "t");
		swal({
			title: title,
			text: text,
			timer: closeTime,
			customClass: 'swal2-modal-'+align,
			showConfirmButton: false
		});
	},
	wrapTipText: function(type, content) {
		if(CommUtil.isEmpty(content)){
			return "";
		}
		//success/error/warning/md5/bad/nice
		type = CommUtil.setDefValue(type, "success");
		log(type+"--"+title);
		var color = "black";
		if("success" == type){
			color = "black";
		}else if("error" == type){
			color = "red";
		}else if("warning" == type){
			color = "orange";
		}else if("md5" == type){
			color = "balck";
		}else if("bad" == type){
			color = "red";
		}else if("nice" == type){
			color = "green";
		}
		return "<font color='"+color+"'>"+content+"</font>";
	},
	alertPrompt: function(title, placeholder, callback, inputType, isRequired){
		isRequired = CommUtil.setDefValue(isRequired, false);
		swal({
			title: title,
			/*html: '<div class="form-group"><input id="input-prompt" type="text" class="form-control" /></div>',*/
			input: inputType,
			showCancelButton: true,
			confirmButtonClass: 'btn btn-success',
			cancelButtonClass: 'btn btn-danger',
			inputPlaceholder: placeholder,
			buttonsStyling: false,
			inputValidator: function(value) {
				return new Promise(function (resolve, reject) {
					if(isRequired){
						if (value === "") {
							$('.swal2-input').focus();
							reject(placeholder);
						}else{
							if(inputType === "email"){
								var emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/
								if (emailRegex.test(value)) {
									resolve()
								} else {
									reject('邮箱格式不正确')
								}
							}else if(inputType === "tel"){
								var telRegex = /^[1][34578][0-9]{9}$/
								if (telRegex.test(value)) {
									resolve()
								} else {
									reject('手机号格式不正确')
								}
							}else{
								resolve();
							}
						}
					}else{
						resolve();
					}
				});
			}
			}).then(function(val) {
			log("prompt----"+val);
			if(typeof callback == 'function'){
				return callback(val);
			}else{
				return val;
			}
		}).catch(swal.noop)
	},
	openPageView: function (title, url, width, height) {
		width = width == undefined ? 600 : width;
		height = height == undefined ? 500 : height;
		width = CommUtil.getResponsiveWidth(width) + 'px';
		height = CommUtil.getResponsiveHeight(height) + 'px';
		var html = '<iframe scrolling="auto" allowtransparency="true" id="swal-iframe-view" name="swal-iframe-view" onload="this.className=\'\';" class="" frameborder="0" src="'+url+'" style="width: 100%; height: '+height+';"></iframe>';
		swal({
			showConfirmButton: false,
			showCancelButton: true,
			width: width,
			html: html
		});
	},
	openPageViewHtml: function (title, html, width, height) {
		width = width == undefined ? 600 : width;
		height = height == undefined ? 500 : height;
		width = CommUtil.getResponsiveWidth(width) + 'px';
		height = CommUtil.getResponsiveHeight(height) + 'px';
		swal({
			showConfirmButton: false,
			showCancelButton: true,
			width: width,
			html: html
		});
	},
}

function dialogConfirmAjaxDel(href, text){
	dialogConfirmAjax(href, text, '确定删除记录？');
}

function dialogConfirmAjax(href, text, tip){
	if(CommUtil.isEmpty(href)){
		dialogTip("请求失败，地址有误", "error");
		return;
	}
	dialogConfirm(text, function(){
		HttpUtil.ajaxAsyncJsonPost(href, {}, function(data){
			dialogTip(data);
			refreshPage(1500);
		});
	}, tip);
}


/**
 * 弹出框
 * @param title
 * @param type 类型：success/error/warning/md5/bad/nice
 */
function dialogAlert(title, type){
	//LayAlert.alert(title, type);
	SwwetAlert.alert(title);
}

/**
 * 提示框
 * @param title
 * @param type 类型：success/error/warning/md5/bad/nice
 * @param autoCloseTime 自动关闭时间（毫秒）
 * @param algin t/r/b/l/lt/lb/rt/rb  默认：auto
 */
function dialogTip(title, type, autoCloseTime, align){
	//LayAlert.alertTip(title, type, autoCloseTime, align);
	SwwetAlert.alertAutoClose(title, type, "", autoCloseTime, align);
}

function dialogCenterTopTip(title, type, timeout){
	// type=['info','success','warning','danger','rose','primary'];
	NotifyUtil.centerTopTip(title, type, null, timeout);
}

function dialogLeftTopTip(title, type, autoCloseTime){
	NotifyUtil.leftTopTip(title, type, autoCloseTime);
}

function dialogRightTopTip(title, type, autoCloseTime){
	NotifyUtil.rightTopTip(title, type, autoCloseTime);
}



var LayAlert = {
	alert: function (title, type) {
		type = type == undefined ? "warning" : type;
		layer.alert(title, {icon: layerGetIcon(type), btn: ["知道了"]});
	},
	alertText: function (title, autoCloseTime) {
		autoCloseTime = autoCloseTime == undefined ? 3000 : autoCloseTime;
		return layer.msg(title, {time: autoCloseTime, offset: '20px'});
	},
	alertTip: function (title, type, autoCloseTime, align) {
		type = type == undefined ? "success" : type;
		autoCloseTime = autoCloseTime == undefined ? 3000 : autoCloseTime;
		align = align == undefined ? "auto" : align;
		return layer.msg(title, {icon: layerGetIcon(type), time: autoCloseTime, offset: '20px'});
	},
	alertConfirm: function (content, href, title, okBtnText, noBtnText) {
		title = title == undefined ? '提示' : title;
		okBtnText = okBtnText == undefined ? '确定' : okBtnText;
		noBtnText = noBtnText == undefined ? '取消' : noBtnText;

		layer.confirm(content, {title: title, icon: 3, btn: [okBtnText, noBtnText]}, function (index) {
			layer.close(index);
			if (typeof href == 'function') {
				return href();
			} else {
				window.location.href = href;
			}
		}, function () {
			console.log("click no btn");
		});
		return false;
	},
	alertPrompt: function (title, callback, inputType) {
		inputType = layerGetInputType(inputType);
		layer.prompt({title: title, formType: inputType}, function (content, index) {
			layer.close(index);
			if (typeof callback == 'function') {
				callback(content);
			}
		});
	},
	closeLoading: function (index) {
		layer.close(index);
	},
	openPageView: function (title, url, width, height) {
		//layer.closeAll();
		width = width == undefined ? 600 : width;
		height = height == undefined ? 500 : height;
		width = CommUtil.getResponsiveWidth(width) + 'px';
		height = CommUtil.getResponsiveHeight(height) + 'px';
		//单窗口模式，层叠置顶
		layer.open({
			id: CommUtil.getRandom() + "-iframe",
			type: 2 //此处以iframe举例
			, title: title
			, area: [width, height]
			, closeBtn: 1
			, shadeClose: true
			, maxmin: true
			, content: url
			, zIndex: layer.zIndex //重点1
			, btn: ['确定']
			, yes: function (index) {
				layer.close(index);
			}
		});
	},

}
/**
 * 提示框
 * @param title
 * @param autoCloseTime 自动关闭时间（毫秒）
 * @param algin t/r/b/l/lt/lb/rt/rb  默认：auto
 */
function dialogTipError(title, autoCloseTime, align){
	return LayAlert.alertTip(title, "error", autoCloseTime, align);
}

/**
 * 提示 文本
 * @param title
 * @param autoCloseTime 自动关闭时间（毫秒）
 */
function dialogTipText(title, autoCloseTime){
	LayAlert.alertText(title, autoCloseTime);
}

/**
 * 右侧 呼出层
 * @param url
 * @param bindId
 */
function dialogPopupRight(url, bindId){
	if(CommUtil.isEmpty(url)){
		dialogTip("脚本缺失 url 参数");
		return;
	}
	if(CommUtil.isEmpty(bindId)){
		dialogTip("脚本缺失 bindId 参数");
		return;
	}
	//该方法 是扩展的layer
	layer.popupRight({
		id: bindId,
		success: function() {
			//log(popup);
			HttpUtil.ajaxAsyncGET(url, function(data){
				$("#"+bindId).html(data);
			});
		}
	})

}

/**
 * prompt层
 * @param title
 * @param placeholder
 * @param callBack 回调函数
 * @param inputType 输入类型 text, password, textarea 默认 text
 */
function dialogPrompt(title, placeholder, callBack, inputType){
	inputType = CommUtil.setDefValue(inputType, "text");
	//LayAlert.alertPrompt(title, callBack, inputType);
	SwwetAlert.alertPrompt(title, placeholder, callBack, inputType, true);
}



var __loadingIndex = 9999999999;
/**
 * 加载提示框
 * @param text 可为空
 * @param autoCloseTime
 */
function dialogLoading(text, autoCloseTime){
	__loadingIndex = layer.load();
	autoCloseTime = autoCloseTime==undefined?1000:autoCloseTime;
	setTimeout(function(){
		layer.close(__loadingIndex); 
	},autoCloseTime );
}

/**
 * 关闭加载提示框
 */
function dialogCloseLoading(){
	LayAlert.closeLoading(__loadingIndex);
}

/**
 * 关闭提示框
 */
function dialogClose(index){
	LayAlert.closeLoading(index);
}

/**
 * 弹出 新页面
 * @param iframeId
 * @param title
 * @param url
 * @param width
 * @param height
 * @param okCall
 * @param cancelCall
 * @param initCall
 */
function dialogOpenPage(iframeId, title, url, width, height, okCall, cancelCall, initCall){
	//layer.closeAll();
	width = width==undefined?550:width;
	height = height==undefined?450:height;
	width = CommUtil.getResponsiveWidth(width)+'px';
	height = CommUtil.getResponsiveHeight(height)+'px';
	//单窗口模式，层叠置顶
	layer.open({
	  id: iframeId,
	  type: 2 //此处以iframe举例
	  ,title: title
	  ,area: [width, height]
	  ,closeBtn: 1
	  ,shadeClose: true
	  ,maxmin: true
	  ,content: url
	  ,btn: ['确定', '取消']
	  ,yes: function(index){
		  if(typeof okCall == 'function'){
			  okCall(index);
		  }
	  }
	  ,btn2: function(){
		  if(typeof cancelCall == 'function'){
			  cancelCall();
		  }
	  }
	  ,zIndex: layer.zIndex //重点1
	  ,success: function(layero){
		  if(typeof initCall == 'function'){
			  initCall();
		  }
	  }
	});
}


/**
 * 弹出 新页面
 * @param iframeId
 * @param title
 * @param url
 * @param width
 * @param height
 * @param okCall
 * @param cancelCall
 * @param initCall
 */
function dialogOpenFullPage(iframeId, title, url, okCall, cancelCall, initCall){
	//layer.closeAll();
	width = CommUtil.getResponsiveWidth(550)+'px';
	height = CommUtil.getResponsiveHeight(450)+'px';
	//单窗口模式，层叠置顶
	var index = layer.open({
		id: iframeId,
		type: 2 //此处以iframe举例
		,title: title
		,closeBtn: 1
		,area: [width, height]
		,shadeClose: true
		,maxmin: true
		,content: url
		,btn: ['确定', '取消']
		,yes: function(index){
			if(typeof okCall == 'function'){
				okCall(index);
			}
		}
		,btn2: function(){
			if(typeof cancelCall == 'function'){
				cancelCall();
			}
		}
		,zIndex: layer.zIndex //重点1
		,success: function(layero){
			if(typeof initCall == 'function'){
				initCall();
			}
		}
	});
	layer.full(index);
	// layer.full()、layer.min()、layer.restore() - 手工执行最大小化（这三个酱油又一次被并列 ==。）一般用于在自定义元素上触发最大化、最小化和全屏。

}


/**
 * 弹出 新页面
 * @param title
 * @param url
 * @param width
 * @param height
 */
function dialogOpenPageView(title, url, width, height){
	//LayAlert.openPageView(title, url, width, height);
	SwwetAlert.openPageView(title, url, width, height);
}

/**
 * 弹出 新页面
 * @param title
 * @param htmlContent
 * @param width
 * @param height
 */
function dialogOpenPageViewHtml(title, htmlContent, width, height){
	SwwetAlert.openPageViewHtml(title, htmlContent, width, height);
}


/**
 * 弹出 新页面
 * @param divId
 * @param title
 * @param htmlContent
 * @param width
 * @param height
 * @param okCall
 * @param initCall
 */
function dialogOpenHtmlContent(divId, title, htmlContent, width, height, okCall, initCall){
	width = width==undefined?450:width;
	height = height==undefined?350:height;
	width = CommUtil.getResponsiveWidth(width)+'px';
	height = CommUtil.getResponsiveHeight(height)+'px';

	//单窗口模式，层叠置顶
	layer.open({
		id: divId,
		type: 1
		,title: title
		,area: [width, height]
		,closeBtn: 1
		,shadeClose: true
		,maxmin: true
		,content: htmlContent
		,btn: ['确定', '取消']
		,yes: function(index){
			if(typeof okCall == 'function'){
				okCall(index);
			}
		}
		,zIndex: layer.zIndex //重点1
		,success: function(layero){
			if(typeof initCall == 'function'){
				initCall();
			}
		}
	});
}
/**
 * 获得 IFrame 索引
 * @param iframeId
 */
function dialogGetIFrameIndex(iframeId){
	var index = layer.getFrameIndex(iframeId);
	return index; //获取窗口索引
}

/**
 * 关闭IFrame
 * @param index
 */
function dialogCloseIFrame(iframeId){
	var index = dialogGetIFrameIndex(iframeId);
	//log("dialogCloseIFrame index:"+index);
	return layer.close(index);
}

/**
 * 获得 IFrame对象
 * @param iframeId
 * @returns iframe
 */
function dialogGetIFrame(iframeId){
	var iframe = $("#"+iframeId+" iframe")[0];
	return iframe;
}


/**
 * @param type success/error/warning/md5/bad/nice
 * @returns {0=感叹号, 1=成功, 2=错误, 3=疑问, 4=加密, 5=坏脸, 6=笑脸}
 */
function layerGetIcon(type){
	log(type);
	if("success" == type){
		return 1;
	}else if("error" == type){
		return 2;
	}else if("warning" == type){
		return 3;
	}else if("md5" == type){
		return 4;
	}else if("bad" == type){
		return 5;
	}else if("nice" == type){
		return 6;
	}else{
		return 0;
	}
}


function layerGetInputType(type){
	if("text" == type){
		return 0;
	}else if("password" == type){
		return 1;
	}else if("textarea" == type){
		return 2;
	}else{
		return 0;
	}
}

/**
 * 复选框选中事件
 * @param attId
 * @param checkedCall
 * @param unCheckedCall
 */
//function checkboxEvent(attId, checkedCall, unCheckedCall){
//	$("#"+attId).click(function(){
//		if($(this).is(":checked")){
//			checkedCall();
//			if(typeof checkedCall == 'function'){
//				checkedCall();
//			}
//		}else{
//			if(typeof unCheckedCall == 'function'){
//				unCheckedCall();
//			}
//		}
//	});
//}



var NotifyUtil = {

	// type=['info','success','warning','danger','rose','primary'];

	showNotice: function (message, type, closeCall, timeout) {
		timeout = CommUtil.setDefValue(timeout, 15000);
		return this.show(message, type, 'top', 'center', timeout, closeCall);
	},
	centerTopTip: function (message, type, closeCall, timeout) {
		type = CommUtil.setDefValue(type, 'info');
		return this.show(message, type, 'top', 'center', timeout, closeCall);
	},
	leftTopTip: function (message, type, closeCall, timeout) {
		type = CommUtil.setDefValue(type, 'info');
		return this.show(message, type, 'top', 'left', timeout, closeCall);
	},
	rightTopTip: function (message, type, closeCall, timeout) {
		type = CommUtil.setDefValue(type, 'info');
		return this.show(message, type, 'top', 'right', timeout, closeCall);
	},
	show: function (message, type, from, align, timeout, closeCall) {
		timeout = CommUtil.setDefValue(timeout, 2000);
		return $.notify({
			icon: "notifications",
			message: message

		},{
			type: type,
			timer: timeout,
			placement: {
				from: from,
				align: align
			},
			onClosed: function(){
				log("notify close");
				if(typeof closeCall == 'function'){
					closeCall();
				}
			},
		});
	},
}